home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / pyshared / launchpadbugs / lptime.py < prev    next >
Text File  |  2008-08-27  |  3KB  |  86 lines

  1. from datetime import datetime
  2. from time import strptime
  3. from email import Utils
  4.  
  5. class LPTime(datetime):
  6.     
  7.     @staticmethod
  8.     def convert_rfc2822_time(time_str):
  9.         """ converts a time-string used in /+text into a time-tuple
  10.         time_str: like 'Tue, 27 Nov 2007 11:15:12 -0000'
  11.         returns: time tuple
  12.         """
  13.         return Utils.parsedate(time_str)[0:6]
  14.     
  15.     @staticmethod
  16.     def convert_text_time(time_str):
  17.         """ converts a time-string used in /+text into a time-tuple
  18.         time_str: like 'Tue, 27 Nov 2007 11:15:12 -0000'
  19.         returns: time tuple
  20.         """
  21.         return strptime(time_str, "%a, %d %b %Y %H:%M:%S -0000")[0:6]
  22.         
  23.     @staticmethod
  24.     def convert_html_time(time_str):
  25.         """ converts a time-string used in html-page into a time-tuple
  26.         time_str: like '2007-11-27 11:15:12 UTC'
  27.         returns: time tuple
  28.         """
  29.         return strptime(time_str, "%Y-%m-%d %H:%M:%S %Z")[0:6]
  30.         
  31.     @staticmethod
  32.     def convert_activity_time(time_str):
  33.         """ converts a time-string used in activity-log-html-page into a time-tuple
  34.         time_str: like '28 Nov 07 20:10'
  35.         returns: time tuple
  36.         """
  37.         return strptime(time_str, "%d %b %y %H:%M")[0:6]
  38.         
  39.     @staticmethod
  40.     def convert_lastcomment_time(time_str):
  41.         """ converts a time-string used in lastzcomment-function into a time-tuple
  42.         time_str: like '2007-12-24'
  43.         returns: time tuple
  44.         """
  45.         return strptime(time_str, "%Y-%m-%d")[0:3]
  46.  
  47.         
  48.     def __new__(cls, time_str):
  49.         t = None
  50.         conv_functions = (  LPTime.convert_rfc2822_time, LPTime.convert_text_time,
  51.                             LPTime.convert_html_time, LPTime.convert_activity_time,
  52.                             LPTime.convert_lastcomment_time)
  53.         for i in conv_functions:
  54.             try:
  55.                 t = i(time_str)
  56.                 if t[0] < 1900:
  57.                     continue
  58.                 break
  59.             except (ValueError, TypeError):
  60.                 continue
  61.         else:
  62.             # there seems to be a problem when parsing timezones
  63.             # in .convert_html_time(), work around this:
  64.             x = time_str.split()
  65.             if x[-1].isalpha():
  66.                 try:
  67.                     x[-1] = "UTC"
  68.                     t = LPTime.convert_html_time(" ".join(x))
  69.                     if t[0] < 1900:
  70.                         t = None
  71.                 except (ValueError, TypeError):
  72.                     pass
  73.         if not t:
  74.             raise ValueError, "Unknown date format (%s)" %time_str
  75.         obj = super(LPTime, cls).__new__(LPTime, *t)
  76.         # Workaround to fix issue with GUI frameworks
  77.         if obj.year < 1900:
  78.             if obj.year < 50:
  79.                 obj = obj.replace(year=obj.year + 2000)
  80.             else:
  81.                 obj = obj.replace(year=obj.year + 1900)
  82.         return obj
  83.         
  84.     def __str__(self):
  85.         return self.strftime("%Y-%m-%d %H:%M:%S UTC")
  86.